<template>
  <div class="view-wrapper">
    <!-- app-container -->
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" style="margin: 10px 0 20px" @click="addClick">添加负责人</el-button>
      </div>

      <el-table v-loading="loading" :data="tabList" row-key="id" :default-expand-all="isExpandAll"
        :tree-props="{ children: 'childList' }" align="center">
        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column prop="dept" label="部门" :show-overflow-tooltip="true" width="260"></el-table-column>
        <el-table-column prop="nickName" label="账号名称"></el-table-column>
        <el-table-column prop="phonenumber" label="手机号" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="authority" label="负责人权限" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dingTalkUserId" label="关联钉钉"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editClick(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="dingdingClick(scope.row)">关联钉钉</el-button>
            <el-button size="mini" type="text" @click="deleteClick(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Dialog :id="id" :pathType='pathType' v-bind="$attrs" v-if="dialogVisible" :visible.sync="dialogVisible"
      @success="_handleSuccess">
    </Dialog>
    <DingdingDialog :id="id" v-bind="$attrs" v-if="dialogDingVisible" :visible.sync="dialogDingVisible"
      @success="_handleSuccess">
    </DingdingDialog>
    <DeleteDialog :id="id" v-bind="$attrs" v-if="dialogDeleteVisible" :visible.sync="dialogDeleteVisible"
      @success="_handleSuccess">
    </DeleteDialog>
  </div>
</template>

<script>
import {
  deleteResponsible,
  getTreeList,
} from "@/api/customer/principalList";
import Dialog from "./components/dialog";
import DeleteDialog from "./components/deleteDialog";
import DingdingDialog from "./components/dingdingDialog";
import ExportExcelButton from "@/components/ExportExcelButton/index";

export default {
  name: "",
  components: {
    Dialog,
    DingdingDialog,
    ExportExcelButton,
    DeleteDialog
  },
  data() {
    return {
      id: '',
      dialogVisible: false,
      dialogDingVisible: false,
      dialogDeleteVisible: false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      // 总条数
      total: 0,
      statusList: [], //状态字典数组

      //表格数据
      tabList: [],
      pathType: 'add',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      imgList: [],
      ids: '',
      isDown: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
    };
  },
  computed: {
    ExportParams() {
      const params = {
        ids: this.ids,
      };
      return params;
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getTreeList().then(response => {
        this.setEmpty(response.result)
        this.tabList = response.result;
        console.log('999', this.tabList)
        // this.total = response.result.total;
        this.loading = false;
      });
    },
    //字段有空就  '-'
    setEmpty(list) {
      list.forEach((item) => {
        if (item.authority == '') {
          item.authority = '-'
        }
        if (item.dingTalkUserId == '') {
          item.dingTalkUserId = '-'
        }
        if (item.childList.length > 0) {
          this.setEmpty(item.childList)
        }
      })
    },
    //新增
    addClick() {
      this.id = null
      this.dialogVisible = true;
      this.pathType = 'add'
    },
    // 编辑
    editClick(row) {
      this.id = row.id
      this.pathType = 'edit'
      this.dialogVisible = true;
    },
    // 关联钉钉
    dingdingClick(row) {
      this.id = row.id
      this.dialogDingVisible = true;
    },
    // 关联钉钉
    deleteClick(row) {
      this.id = row.id
      
      if (row.authority == '-' || row.authority == '') {
        this.$confirm(`删除该负责人?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var data = {
            id: row.id,
          }
          this.loading = true
          deleteResponsible(data).then(res => {
            setTimeout(() => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getList()
            }, 1000);
          }).finally(() => {
          })
        }).catch(() => { });
      } else {
        this.dialogDeleteVisible = true;
      }
    },
    _handleSuccess(e) {
      //成功回调
      // this.resetQuery()
      this.getList()
    },
    cancelShow(row) {
      var txt = ''
      var showFlag = ''
      if (row.showFlag == 1) {
        txt = '取消首页展示'
        showFlag = 2
      } else {
        txt = '设为首页展示'
        showFlag = 1
      }
      this.$confirm(`${txt}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = {
          id: row.id,
          showFlag: showFlag,
        }
        changeShowFlag(data).then(response => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.resetQuery()
        });
      }).catch(() => { });
    },
  }
};

</script>
<style scoped>
.head {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px 0;
}

.listBox {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px;
  margin-top: 10px;
}

.listBox_head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.img_box {
  width: 100px;
  height: 80px;
  border-radius: 4px;
}
</style>
